package com.siyuan.blog.web.system;

import com.baomidou.mybatisplus.plugins.Page;
import com.siyuan.blog.domain.system.Role;
import com.siyuan.blog.service.system.UserService;
import com.siyuan.blog.common.vo.JSONResponse;
import com.siyuan.blog.common.vo.LayTableResponse;
import com.siyuan.blog.domain.system.User;
import com.siyuan.blog.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;


    /**
     * 用户管理添加页
     * @return
     */
    @RequestMapping("/ui/add")
    public String addUI(ModelMap modelMap){
        List<Role> roles = roleService.selectList(null);
        modelMap.put("roles",roles);
        return "system/user/add";
    }

    /**
     * 用户管理添加页
     * @return
     */
    @RequestMapping("/ui/edit")
    public String editUI(Integer id, ModelMap modelMap){
        User user = userService.selectById(id);
        List<Role> roles = roleService.selectList(null);
        modelMap.put("roles",roles);
        modelMap.put("user",user);
        return "system/user/edit";
    }

    /**
     * 用户管理列表页
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI(){
        return "system/user/list";
    }


    /**
     * 列表页数据接口
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse list(int page, int limit){
        Page<User> resourcesPage = userService.selectUserPage(new Page<User>(page, limit),null);
        return new LayTableResponse(resourcesPage.getRecords(),resourcesPage.getTotal());
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(User user){
        userService.insert(user);
        return "redirect:/user/ui/list";
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/ui/list";
    }


    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Integer> ids){
        userService.deleteBatchIds(ids);
        return "redirect:/user/ui/list";
    }


    /**
     * 启用/禁用
     * @status  1:启用  0 ：禁用
     * @return
     */
    @RequestMapping(path = "/status")
    public String delete(Integer id){
        userService.modifyStatus(id);
        return "redirect:/user/ui/list";
    }


    /**
     * 变更角色界面
     * @id
     * @return
     */
    @RequestMapping(path = "/ui/changerole")
    public String changeRoleUI(Integer id,ModelMap modelMap){
        User user = userService.selectById(id);
        List<Role> roles = roleService.selectList(null);
        modelMap.put("roles",roles);
        modelMap.put("user",user);
        return "system/user/changerole";
    }

    /**
     * 修改角色
     * @param id
     * @param roleId
     * @return
     */
    @RequestMapping(path = "/changerole")
    @ResponseBody
    public JSONResponse changeRoleUI(Integer id,Integer roleId){
        userService.modifyRole(id,roleId);
        return JSONResponse.success("OK");
    }



}
